﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using XuanFe.Database.Model;

namespace XuanFe.Database.DAL
{
    public class ModuleDAL
    {
        private const string SQL_SELECT_WS_MODULE = "select MOD_MODULE_ID,MOD_MODULE_NAME,MOD_PARENT_MODULE_ID,MOD_SORT_INDEX from WS_MODULE where 1=1";
        private const string SQL_SELECT_WS_MODULE_AND_ROLE = "select MOD_MODULE_ID,MOD_MODULE_NAME,MOD_PARENT_MODULE_ID,MOD_SORT_INDEX from WS_MODULE,WS_ROLE_MODULE where ROM_MODULE_ID=MOD_MODULE_ID";
        private const string SQL_INSERT_WS_MODULE = "insert into WS_MODULE(MOD_MODULE_ID,MOD_MODULE_NAME,MOD_PARENT_MODULE_ID,MOD_SORT_INDEX) values(@ModuleId,@ModuleName,@ParentModuleId,@SortIndex)";

        private const string SQL_WHERE_MODULE_ID = " and MOD_MODULE_ID=@ModuleId ";
        private const string SQL_WHERE_PARENT_MODULE_ID = " and MOD_PARENT_MODULE_ID=@ParentModuleId ";
        private const string SQL_WHERE_ROLE_ID = " and ROM_ROLE_ID=@RoleId ";

        private const string SQL_ORDER_BY_SORT_INDEX = " order by MOD_SORT_INDEX ";
        private const string SQL_ORDER_BY_PARENT_MODULE_AND_SORT_INDEX = " order by MOD_PARENT_MODULE_ID,MOD_SORT_INDEX ";

        private const string PARM_MODULE_ID = "@ModuleId";
        private const string PARM_MODULE_NAME = "@ModuleName";
        private const string PARM_PARENT_MODULE_ID = "@ParentModuleId";
        private const string PARM_SORT_INDEX = "@SortIndex";
        private const string PARM_ROLE_ID = "@RoleId";

        private static ModuleDAL instance = new ModuleDAL();
        /// <summary>
        /// 初始化实例
        /// </summary>
        /// <returns></returns>
        public static ModuleDAL Instance
        {
            get
            {
                return instance;
            }
        }

        public ModuleModel Get(string id)
        {
            StringBuilder sbSQL = new StringBuilder(SQL_SELECT_WS_MODULE);
            sbSQL.Append(SQL_WHERE_MODULE_ID);

            List<MySqlParameter> parms = new List<MySqlParameter>();
            parms.Add(new MySqlParameter(PARM_MODULE_ID, MySqlDbType.String) { Direction = ParameterDirection.Input, Value = id });

            using (DataSet ds = MySQLHelper.Instance.ExecuteDataSet(MySQLHelper.Instance.ConnectionString, CommandType.Text, sbSQL.ToString(), parms.ToArray()))
            {
                if (ds.Tables[0].Rows.Count > 0)
                {
                    DataRow t = ds.Tables[0].Rows[0];
                    return new ModuleModel()
                    {
                        Id = Convert.ToString(t.ItemArray[0]),
                        Name = Convert.ToString(t.ItemArray[1]),
                        ParentId = Convert.ToString(t.ItemArray[2]),
                        SortIndex = Convert.ToInt32(t.ItemArray[3])
                    };
                }
                return null;
            }
        }
        public List<ModuleModel> Gets()
        {
            StringBuilder sbSQL = new StringBuilder(SQL_SELECT_WS_MODULE);
            sbSQL.Append(SQL_WHERE_PARENT_MODULE_ID);
            sbSQL.Append(SQL_ORDER_BY_PARENT_MODULE_AND_SORT_INDEX);

            using (DataSet ds = MySQLHelper.Instance.ExecuteDataSet(MySQLHelper.Instance.ConnectionString, CommandType.Text, SQL_SELECT_WS_MODULE, null))
            {
                DataTable dt = ds.Tables[0];
                List<ModuleModel> list = dt.AsEnumerable().Select(t => new ModuleModel
                {
                    Id = Convert.ToString(t.ItemArray[0]),
                    Name = Convert.ToString(t.ItemArray[1]),
                    ParentId = Convert.ToString(t.ItemArray[2]),
                    SortIndex = Convert.ToInt32(t.ItemArray[3])
                }).ToList<ModuleModel>();

                return list;
            }
        }
        public List<ModuleModel> Gets(string parentId)
        {
            StringBuilder sbSQL = new StringBuilder(SQL_SELECT_WS_MODULE);
            sbSQL.Append(SQL_WHERE_PARENT_MODULE_ID);
            sbSQL.Append(SQL_ORDER_BY_SORT_INDEX);

            List<MySqlParameter> parms = new List<MySqlParameter>();
            parms.Add(new MySqlParameter(PARM_PARENT_MODULE_ID, MySqlDbType.String) { Direction = ParameterDirection.Input, Value = parentId });

            using (DataSet ds = MySQLHelper.Instance.ExecuteDataSet(MySQLHelper.Instance.ConnectionString, CommandType.Text, sbSQL.ToString(), parms.ToArray()))
            {
                DataTable dt = ds.Tables[0];
                List<ModuleModel> list = dt.AsEnumerable().Select(t => new ModuleModel
                {
                    Id = Convert.ToString(t.ItemArray[0]),
                    Name = Convert.ToString(t.ItemArray[1]),
                    ParentId = Convert.ToString(t.ItemArray[2]),
                    SortIndex = Convert.ToInt32(t.ItemArray[3])
                }).ToList<ModuleModel>();

                return list;
            }
        }
        public List<ModuleModel> GetFromRole(string roleId)
        {
            StringBuilder sbSQL = new StringBuilder(SQL_SELECT_WS_MODULE_AND_ROLE);
            sbSQL.Append(SQL_WHERE_ROLE_ID);
            sbSQL.Append(SQL_ORDER_BY_SORT_INDEX);

            List<MySqlParameter> parms = new List<MySqlParameter>();
            parms.Add(new MySqlParameter(PARM_ROLE_ID, MySqlDbType.String) { Direction = ParameterDirection.Input, Value = roleId });

            using (DataSet ds = MySQLHelper.Instance.ExecuteDataSet(MySQLHelper.Instance.ConnectionString, CommandType.Text, sbSQL.ToString(), parms.ToArray()))
            {
                DataTable dt = ds.Tables[0];
                List<ModuleModel> list = dt.AsEnumerable().Select(t => new ModuleModel
                {
                    Id = Convert.ToString(t.ItemArray[0]),
                    Name = Convert.ToString(t.ItemArray[1]),
                    ParentId = Convert.ToString(t.ItemArray[2]),
                    SortIndex = Convert.ToInt32(t.ItemArray[3])
                }).ToList<ModuleModel>();

                return list;
            }
        }
        public int Insert(DbTransaction trans, ModuleModel model)
        {
            List<MySqlParameter> parms = new List<MySqlParameter>();
            parms.Add(new MySqlParameter(PARM_MODULE_ID, MySqlDbType.String) { Direction = ParameterDirection.Input, Value = model.Id });
            parms.Add(new MySqlParameter(PARM_MODULE_NAME, MySqlDbType.String) { Direction = ParameterDirection.Input, Value = model.Name });
            parms.Add(new MySqlParameter(PARM_PARENT_MODULE_ID, MySqlDbType.String) { Direction = ParameterDirection.Input, Value = model.ParentId });
            parms.Add(new MySqlParameter(PARM_SORT_INDEX, MySqlDbType.Int32) { Direction = ParameterDirection.Input, Value = model.SortIndex });

            return MySQLHelper.Instance.ExecuteNonQuery(trans, CommandType.Text, SQL_INSERT_WS_MODULE, parms.ToArray());
        }
    }
}
